﻿using System; 
using System.Data.SqlClient;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using System.Reflection;
using System.Data;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataObjectColumnHelpSettingELBDao : DaoService<DataObjectColumnHelpSetting>
    { 
        public override string TableName => "fas_dataobjectcolumnhelpsetting";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataObjectID";

        //public override List<string> ExceptColumnName {get;} = new List<string>
        //{
        //    "DefaultValueType",
        //};

        public DataObjectColumnHelpSettingELBDao() : base()
        {
            DBName = "TFAS";
        }

        public override DataObjectColumnHelpSetting ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("DataSourceType"))
                columns.Remove("DataSourceType");

            var data = base.ConvertToObject(columns, row);
            var strDataSourceType = row["DataSourceType"]?.ToString();
            if (!string.IsNullOrEmpty(strDataSourceType))
                data.DataSourceType = (DataSourceType)Enum.Parse(typeof(DataSourceType), strDataSourceType);
            else
                data.DataSourceType = DataSourceType.DataQuery;

            return data;
        }

        public override Dictionary<string, object> GetDataMapping(DataObjectColumnHelpSetting data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("DataSourceType");

            mapping.Add("DataSourceType", (int)data.DataSourceType);

            return mapping;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="doId"></param>
        /// <returns></returns>
        public List<DataObjectColumnHelpSetting> GetColumnHelpSettings(string doId)
        {
            var result = new List<DataObjectColumnHelpSetting>();
            if (string.IsNullOrEmpty(doId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{doId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataObjectColumnHelpSetting));
            }
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="settings"></param>
        public void CreateColumnHelpSetting(List<DataObjectColumnHelpSetting> settings)
        {
            try
            {
                base.Add(settings);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateJoinError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="doId"></param>
        public void DeleteColumnHelpSetting(string doId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, doId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.DeleteColumnError, e.Message, e);
            }
        }

    }
}
